package lumingweihua.future.cn.lumingweihua.network;

import android.app.Activity;
import android.text.TextUtils;
import android.widget.Toast;

import com.zhusx.core.interfaces.IHttpResult;
import com.zhusx.core.network.HttpRequest;
import com.zhusx.core.utils._Encryptions;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

import lumingweihua.future.cn.baselibgxh.base.MyApplication;
import lumingweihua.future.cn.lumingweihua.ui.BaseAppFragment;
import lumingweihua.future.cn.lumingweihua.ui.BaseCompatActivity;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.CountEntity;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.InfoData;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.InfoDetailData;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.NotifyData;
import lumingweihua.future.cn.lumingweihua.ui.message.domain.TransportationStatusData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.CarDetailData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.CarNameData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.CarSourceData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.GoodsSourceData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.LinkNameData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.ModifyBossData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.MyCarData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.PhoneData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.PushData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.ReviewData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.RushDetailData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.ShareData;
import lumingweihua.future.cn.lumingweihua.ui.mine.domain.UserInfoData;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.OrderData;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.SupplyData;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.WuliuData;
import lumingweihua.future.cn.lumingweihua.ui.order.domain.YunShuData;
import lumingweihua.future.cn.lumingweihua.widget.dialog.LoadingDialog;

/**
 * Created by z1986 on 2018/3/18.
 */

public class LoadData<T> extends BaseLoadData<LoadData.Api, T> {
    private final static String KEY = "1e56c95504a9a846e4c7043704a20f25";
    Activity activity;
    LoadingDialog progressDialog;
    public static final int PAGE_ITME = 10;

    public enum Api {
        测试("_app_check_001"),
        短信发送接口("_sms_001"),
        消息系统消息列表("_message_001"),
        个人中心头像昵称修改("_useramend_001"),
        资讯列表("_informationlist_001"),
        app分享("_share_001"),
        资讯详情("_information_details_001"),
        货主认证进度("_progress_001"),
        个人中心我的抢单("_userindent_001"),
        个人中心我的车辆信息修改("_amend_usercar_001"),
        个人中心我的车辆信息修改_手机号("_amend_usercar_001"),
        个人中心货主认证("_goods_enter_001"),
        个人中心("_userinfo_001"),
        个人中心我的车辆列表("_user_car_001"),
        个人中心新增车辆业务员列表("_professionallist_001"),
        个人中心我的车辆新增("_add_usercar_001"),
        个人中心车辆删除("_usercar_delete_001"),
        个人中心我发布的车源("_user_carsource_001"),
        个人中心我发布的货源("_user_goods_001"),
        个人中心热线号码("_hot_line_001"),
        个人中心使用指南("_operating_guide_001"),
        //        消息系统消息详情("_message_particulars_001"),
        货主订单("_owner_indent_001"),
        货主订单运输信息("_transportation_001"),
        货主订单运输详情("_transportation_details_001"),
        货主订单订单详情("_user_goodsdetails_001"),
        系统通知数量("_shuliangs_001"),
        消息运输动态列表("_message_dynamic_001"),
        消息运输动态列表清空("_delete_dynamic_001"),
        消息系统消息清空("_deletemessage_001"),
        微信端抢单详情("_wxindent_details_001"),
        微信端车辆详情("_wxcar_details_001"),
        个人中心是否绑定业务员("_yewu_001"),
        个人中心绑定业务员("_addyewu_001"),
        个人中心新增车辆罐体类型选择列表("_tanklist_001"),
        个人中心我发布的货源清理("_delete_user_goods_001"),
        货主认证信息显示("_xianshi_good_001"),
        货主认证信息修改("_save_goods_001");
        public String apiCode;

        Api(String api) {
            this.apiCode = api;
        }
    }

    @Override
    RequestParam getHttpParams(Api api, Object... objects) throws JSONException {
        RequestParam httpParams = new RequestParam();
        Map<String, String> map = new TreeMap<>();
        httpParams.url = "http://lm.feiyouce.com/";
        switch (api) {
            case 测试:
                map.put("app_version", "1.0.0");
                map.put("platform", "101");
                break;
            case 消息系统消息列表:
                httpParams.returnClass = NotifyData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 个人中心头像昵称修改:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put(valueOf(objects[0]), valueOf(objects[1]));
                break;
            case 个人中心:
                httpParams.returnClass = UserInfoData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 资讯列表:
                httpParams.returnClass = InfoData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 资讯详情:
                httpParams.returnClass = InfoDetailData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("id", valueOf(objects[0]));
                break;
            case 货主订单:
                httpParams.returnClass = OrderData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 个人中心我的车辆列表:
                httpParams.returnClass = MyCarData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 个人中心热线号码:
                httpParams.returnClass = PhoneData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 个人中心使用指南:
                httpParams.returnClass = String.class;
                break;
            case 个人中心我的抢单:
                httpParams.returnClass = PushData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("page", valueOf(_getNextPage()));
                map.put("size", valueOf(PAGE_ITME));
                map.put("status", valueOf(objects[0]));
                break;
            case 个人中心我发布的车源:
                httpParams.returnClass = CarSourceData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("status", valueOf(objects[0]));
                map.put("page", valueOf(_getNextPage()));
                map.put("size", valueOf(PAGE_ITME));
                break;
            case 个人中心我发布的货源:
                httpParams.returnClass = GoodsSourceData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("status", valueOf(objects[0]));
                map.put("page", valueOf(_getNextPage()));
                map.put("size", valueOf(PAGE_ITME));
                break;
            case 货主订单运输信息:
                httpParams.returnClass = YunShuData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("good_num", valueOf(objects[0]));
                break;
            case 货主订单订单详情:
                httpParams.returnClass = SupplyData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("good_num", valueOf(objects[0]));
                break;
            case 货主订单运输详情:
                httpParams.returnClass = WuliuData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("car_id", valueOf(objects[0]));
                map.put("good_num", valueOf(objects[1]));
                break;
            case 微信端车辆详情:
                httpParams.returnClass = CarDetailData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("id", valueOf(objects[0]));
                break;
            case 系统通知数量:
                httpParams.returnClass = CountEntity.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 消息运输动态列表:
                httpParams.returnClass = TransportationStatusData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 消息运输动态列表清空:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 消息系统消息清空:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 短信发送接口:
                httpParams.returnClass = String.class;
                map.put("mobile", valueOf(objects[0]));
                break;
            case 个人中心我的车辆新增:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("car_name", valueOf(objects[0]));
                map.put("car_mobile", valueOf(objects[1]));
                map.put("code", valueOf(objects[2]));
                map.put("license", valueOf(objects[3]));
                map.put("type", valueOf(objects[4]));
                map.put("load", valueOf(objects[5]));
                map.put("drive_img", valueOf(objects[6]));
                map.put("run_img", valueOf(objects[7]));
                break;
            case 个人中心新增车辆业务员列表:
                httpParams.returnClass = LinkNameData.class;
                httpParams.isList = true;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 个人中心货主认证:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("linkname", valueOf(objects[0]));
                map.put("id_num", valueOf(objects[1]));
                map.put("company_name", valueOf(objects[2]));
                map.put("profession_mobile", valueOf(objects[3]));
                map.put("hand_img", valueOf(objects[4]));
                map.put("permit", valueOf(objects[5]));
                break;
            case 个人中心我的车辆信息修改_手机号:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("id", valueOf(objects[0]));
                map.put("car_mobile", valueOf(objects[1]));
                map.put("code", valueOf(objects[2]));
                break;
            case 个人中心我的车辆信息修改:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("id", valueOf(objects[0]));
                map.put("license", valueOf(objects[1]));
                map.put("type", valueOf(objects[2]));
                map.put("load", valueOf(objects[3]));
                map.put("car_name", valueOf(objects[4]));
                if (objects[5] != null) {
                    map.put("drive_img", valueOf(objects[5]));
                }
                if (objects[6] != null) {
                    map.put("run_img", valueOf(objects[6]));
                }
                break;
            case 个人中心车辆删除:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("id", valueOf(objects[0]));
                break;
            case 货主认证进度:
                httpParams.returnClass = ReviewData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 微信端抢单详情:
                httpParams.returnClass = RushDetailData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("id", valueOf(objects[0]));
                break;
            case app分享:
                httpParams.returnClass = ShareData.class;
                break;
            case 个人中心是否绑定业务员:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 个人中心绑定业务员:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("profession_mobile", valueOf(objects[0]));
                break;
            case 个人中心新增车辆罐体类型选择列表:
                httpParams.isList = true;
                httpParams.returnClass = CarNameData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 个人中心我发布的货源清理:
                httpParams.returnClass = String.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("good_num", valueOf(objects[0]));
                break;
            case 货主认证信息显示:
                httpParams.returnClass = ModifyBossData.class;
                map.put("userToken", MyApplication.getInstance().getUserToken());
                break;
            case 货主认证信息修改:
                map.put("userToken", MyApplication.getInstance().getUserToken());
                map.put("linkname", valueOf(objects[0]));
                map.put("id_num", valueOf(objects[1]));
                map.put("company_name", valueOf(objects[2]));
                map.put("profession_mobile", valueOf(objects[3]));
                if (objects[4] != null) {
                    map.put("hand_img", valueOf(objects[4]));
                }
                if (objects[5] != null) {
                    map.put("permit", valueOf(objects[5]));
                }
                break;
        }
        JSONObject json = new JSONObject();
        String param = "";
        try {
            json.put("apiCode", api.apiCode);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        if (!map.isEmpty()) {
            for (String key : map.keySet()) {
                String value = map.get(key);
                json.put(key, value);
                if (!TextUtils.isEmpty(value)) {
                    param += value;
                }
            }
        }
        String token = _Encryptions.encodeHex(_Encryptions.Digest.MD5, api.apiCode + param + KEY);
        json.put("token", token);
        Map<String, String> params = new HashMap<>();
        params.put("json", json.toString());
        httpParams.map = params;
        return httpParams;
    }

    @Override
    protected void __onStart(Api api, HttpRequest<Object> httpRequest) {
        switch (api) {
            case 个人中心头像昵称修改:
            case 个人中心我的车辆信息修改:
            case 个人中心我的车辆信息修改_手机号:
            case 个人中心新增车辆业务员列表:
            case 个人中心热线号码:
            case 个人中心车辆删除:
            case 消息系统消息清空:
            case 消息运输动态列表清空:
            case 短信发送接口:
            case 个人中心是否绑定业务员:
            case 个人中心货主认证:
            case 个人中心绑定业务员:
            case 个人中心我的车辆新增:
            case 个人中心新增车辆罐体类型选择列表:
            case 个人中心我发布的货源清理:
            case 货主认证信息修改:
                if (progressDialog == null) {
                    progressDialog = new LoadingDialog(activity);
                }
                progressDialog.show();
                break;
        }
    }

    @Override
    protected void __onError(Api api, HttpRequest<Object> httpRequest, IHttpResult<T> iHttpResult, boolean b, String s) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
        if (!TextUtils.isEmpty(s)) {
            Toast.makeText(activity, s, Toast.LENGTH_SHORT).show();
        }
    }


    @Override
    protected void __onComplete(Api api, HttpRequest<Object> httpRequest, IHttpResult<T> iHttpResult) {
        if (progressDialog != null && progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }

    public LoadData(Api api, BaseCompatActivity activity) {
        super(api, activity);
        this.activity = activity;
    }

    public LoadData(Api api, BaseAppFragment activity) {
        super(api, activity);
        this.activity = activity.getActivity();
    }
}
